package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domian.Dish;
import com.itheima.domian.vo.DishVo;
import com.itheima.domian.Flavor;
import com.itheima.domian.vo.QueryVo;
import com.itheima.mapper.admin.DishMapper;
import com.itheima.service.admin.DishService;
import com.itheima.service.admin.FlavorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 12441
 * @Date 2022/8/11 15:13
 * @Version 1.0
 */
@Service
@Slf4j
public class DishServiceImpl  extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorService flavorService;

    /**
     * @param page         分页参数
     * @param queryWrapper 查询条件
     * @return 分页后的数据
     */
    @Override
    public IPage<QueryVo> findByPage(Page<QueryVo> page, QueryWrapper<QueryVo> queryWrapper) {
        return dishMapper.findByPage(page, queryWrapper);
    }

    @Transactional
    public void addDish(DishVo dishVo) {

        this.save(dishVo);

        //加工：取出dishVo的flavor属性添加id
        //Stream流map方法可以将字符串转换为对象接收，使代码的灵活性更高
        Long dishId = dishVo.getId();

        log.info("============"+dishId);

        List<Flavor> flavors = dishVo.getFlavors();
        flavors = flavors.stream().map((item)-> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        log.info(flavors.toString());

        flavorService.saveBatch(flavors);



    }

    @Override
    public DishVo getByIdWithFlavor(Long id) {
        //查询菜品
        Dish dish=this.getById(id);

        DishVo dishVo = new DishVo();

        BeanUtils.copyProperties(dish,dishVo);

        //查询口味数组，赋值
        LambdaQueryWrapper<Flavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Flavor::getDishId,dish.getId());
        List<Flavor> flavors = flavorService.list(queryWrapper);
        dishVo.setFlavors(flavors);
        return dishVo;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishVo dishVo) {
        this.updateById(dishVo);

        //清除原先的口味
        LambdaQueryWrapper<Flavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Flavor::getDishId,dishVo.getId());
        flavorService.remove(queryWrapper);

        //添加新的口味信息
        List<Flavor> flavors = dishVo.getFlavors();
        flavors = flavors.stream().map((item)-> {
            item.setDishId(dishVo.getId());
            return item;
        }).collect(Collectors.toList());


        flavorService.saveBatch(flavors);




    }
}
